//
//  MoveCollectionController.swift
//  HHSwift
//
//  Created by 华宏 on 2020/1/27.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit

class MoveCollectionController: UIViewController {

    var dataArray = [UIColor]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Cell移动"
        
        for _ in stride(from: 0, through: 30, by: 1) {
            let color = UIColor.init(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)
            dataArray.append(color)
        }
        
        self.view.addSubview(self.collectionView)
        
    }
    

    lazy var collectionView: UICollectionView = {
        let layout = HHMovedLayout()
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

        let columnCount = 3
        let space = CGFloat(columnCount - 1) * layout.minimumInteritemSpacing
        let itemWidth = (self.view.bounds.size.width - layout.sectionInset.left - layout.sectionInset.right - space) / CGFloat(columnCount)
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        
        
        let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.alwaysBounceVertical = true
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        collectionView.keyboardDismissMode = .onDrag
        return collectionView
    }()

}

extension MoveCollectionController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,HHMovedLayoutDelegate,HHMovedLayoutDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.contentView.backgroundColor = dataArray[indexPath.item]
        
        return cell
    }
    
    //MARK: - HHMovedLayoutDataSource
    func collectionView(_ collectionView:UICollectionView?,willMove sourceIndexPath:IndexPath,destinationIndexPath:IndexPath) -> Void{
        
    }
    
    func collectionView(_ collectionView:UICollectionView?,didMove sourceIndexPath:IndexPath,destinationIndexPath:IndexPath) -> Void{
       
        let color = dataArray[sourceIndexPath.item]
        dataArray.remove(at: sourceIndexPath.item)
        dataArray.insert(color, at: destinationIndexPath.item)
        self.collectionView.reloadData()
        
    }
    
    func collectionView(_ collectionView:UICollectionView?,canMove indexPath:IndexPath) -> Bool{
        
        return indexPath.item < dataArray.count
    }
    
    func collectionView(_ collectionView:UICollectionView?,canMove sourceIndexPath:IndexPath,destinationIndexPath:IndexPath) -> Bool{
        return sourceIndexPath.item < dataArray.count && destinationIndexPath.item < dataArray.count
    }
    
    //MARK: - HHMovedLayoutDelegate
    
    func collectionView(_ collectionView:UICollectionView?,willBeginDragging layout:UICollectionViewLayout,indexPath:IndexPath) -> Void{
        
    }
       
    func collectionView(_ collectionView:UICollectionView?,didBeginDragging layout:UICollectionViewLayout,indexPath:IndexPath) -> Void{
        
    }

    func collectionView(_ collectionView:UICollectionView?,willEndDragging layout:UICollectionViewLayout,indexPath:IndexPath) -> Void{
        
    }

    func collectionView(_ collectionView:UICollectionView?,didEndDragging layout:UICollectionViewLayout,indexPath:IndexPath) -> Void{
        
    }

}
